use std::ptr;
use alloc_testing::{alloc_test_get_header, alloc_test_overwrite, alloc_test_malloc, alloc_test_free, alloc_test_realloc, alloc_test_calloc, alloc_test_strdup, alloc_test_set_limit, alloc_test_get_allocated};

type UnitTestFunction = unsafe extern "C" fn();

unsafe fn run_test(test: UnitTestFunction) {
    // Turn off any allocation limits that may have been set
    // by a previous test.
    alloc_test_set_limit(-1);

    // Run the test
    test();

    // Check that all memory was correctly freed back during
    // the test.
    assert!(alloc_test_get_allocated() == 0);
}

pub unsafe fn run_tests(tests: *const UnitTestFunction) {
    let mut i = 0;
    while !tests.offset(i).read().is_null() {
        run_test(tests.offset(i).read());
        i += 1;
    }
}
